package com.buyouchina.service.area.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.buyouchina.bean.area.Province;
import com.buyouchina.service.area.ProvinceService;
import com.buyouchina.service.base.DaoSupport;

@Service
@Transactional
public class ProvinceServiceBean extends DaoSupport<Province> implements ProvinceService{

		String qlString;
	
		public Province findByName(String name){
			
			qlString="select p from Province p where p.name=?1";
			Query query=em.createQuery(qlString).setParameter(1,name);
			return (Province) query.getSingleResult();
		}
		
		@SuppressWarnings("unchecked")
		public List<Province> list(String sql){
			return em.createQuery(sql).getResultList();
		}
		
		public List<Province> list(){
			qlString="from Province p where p.fullAddress is null";
			return list(qlString);
		}
		
		public List<Province> list(List<Province> provinces){
			qlString="from Province p where p.id=?1";
			List<Object> params=new ArrayList<Object>();
			for(int i=1;i<provinces.size()&&provinces.size()>1;i++){
				params.add(provinces.get(0).getId());
				qlString+="or p.id=?"+i+1;
			}
			params.add(provinces.get(0).getId());
			return list(qlString,params.toArray());
		}

		public String getCityId(Integer areaId) {
			Province province=this.find(areaId);
			return province.getCityid();
		}
		public String getProvinceId(Integer areaId) {
			Province province=this.find(areaId);
			return province.getProvinceid();
		}
	
}
